package org.example.web;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class TriggerController {
    @Autowired
    private JobLauncher jobLauncher;
    @Autowired
    private Job importBandJob;

    @SneakyThrows
    @GetMapping("/start")
    public ResponseEntity<?> triggerByRequest() {
        log.info("Job trigger by request.");
        JobParameters jobParameters = new JobParametersBuilder().addLong(
                "time", System.currentTimeMillis()).toJobParameters();
        jobLauncher.run(importBandJob, jobParameters);

        return new ResponseEntity<>( "Job Started.", HttpStatus.OK);
    }
}
